using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Reflection;

/// <summary>
/// Summary description for PageInfoLoad
/// </summary>
public class PageInfoLoad
{
    HDDatabase HDD = new HDDatabase();
    LoadEditPage LEP = new LoadEditPage();
    public PageInfoLoad()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    public DataSet GetPageSetPageCtrlInfo(string strPagesetRowId)
    {
        DataSet dsePageInfo = new DataSet();
        DataTable dtaObj;
        string page_id = "";

        string strSql = "select * from sy_pgs where pageset_id='" + strPagesetRowId.Replace("'", "''") + "'";
        DataTable dtPageSet = CommonFunctions.GetTableFromPageInfo(strPagesetRowId, "sy_pgs").Copy();
        if (dtPageSet == null || dtPageSet.Rows.Count == 0)
            dsePageInfo.Tables.Add(HDD.GetDataTable(strSql, "PageSet").Copy());
        else
        {
            dtPageSet.TableName = "PageSet";
            dsePageInfo.Tables.Add(dtPageSet.Copy());
        }

        string sAF_ROW_ID = dsePageInfo.Tables["PageSet"].Rows[0]["af_row_id"].ToString();

        string strPageSetId = sAF_ROW_ID;

        //Query needs to modify using innerjoin and needs to store with sql store
        //strSql = "select a.* from sy_pg a,sy_pgs_mem b where a.page_id=b.page_id and b.pageset_id ='" + strPagesetRowId.Replace("'", "''") + "' order by b.page_seq";
        //strSql = "select a.* from sy_pg a,sy_pgs_mem b where a.page_id=b.page_id and b.af_row_id ='" + sAF_ROW_ID + "' order by b.page_seq";
        dtaObj = CommonFunctions.GetTableFromPageInfo(strPagesetRowId, "sy_pg").Copy();
        if (dtaObj == null || dtaObj.Rows.Count == 0)
        {
            strSql = "select a.* from sy_pg a,sy_pgs_mem b inner join sy_pgs c on c.af_row_id = b.pageset_id where a.page_id=b.page_id and b.pageset_id ='" + sAF_ROW_ID + "' order by b.page_seq";
            dtaObj = HDD.GetDataTable(strSql, "Page").Copy();
        }
        else
            dtaObj.TableName = "Page";
        dsePageInfo.Tables.Add(dtaObj.Copy());
        if (dtaObj != null)
        {
            if (dtaObj.Rows.Count > 0)
            {
                page_id = dtaObj.Rows[0]["af_row_id"].ToString();
            }
        }
  

        //strSql = "select * from sy_pg_key a,sy_pgs_mem b where a.page_id=b.page_id and b.pageset_id ='" + strPagesetRowId.Replace("'", "''") + "'";
        //strSql = "select * from sy_pg_key a,sy_pgs_mem b where a.page_id=b.page_id and b.af_row_id ='" + sAF_ROW_ID + "'";
        //strSql = "select * from sy_pg_key a,sy_pgs_mem b where a.page_id=b.page_id and b.af_row_id ='" + sAF_ROW_ID + "'";
        //strSql = "select * from sy_pg_key a inner join sy_pg c on c.af_row_id = a.page_id inner join sy_pgs_mem b on c.page_id=b.page_id and b.pageset_id ='" + sAF_ROW_ID + "'";

        //dsePageInfo.Tables.Add(HDD.GetDataTable(strSql, "PageKeys").Copy());
        DataTable dtPageKey = new DataTable("PageKeys");
        //dsePageInfo.Tables.Add(dtPageKey);
        //strSql = "select * from sy_obj_mas_detail a inner join sy_pg c on c.af_row_id = a.page_id inner join sy_pgs_mem b on c.page_id=b.page_id and b.pageset_id ='" + sAF_ROW_ID + "'";
        //DataTable dt = HDD.GetDataTable(strSql);

        dsePageInfo.Tables.Add(GetCtrlInfo(dtaObj, ref dtPageKey, strPagesetRowId));
        dtPageKey.TableName = "PageKeys";
        dsePageInfo.Tables.Add(dtPageKey.Copy());

        // Added by srivatsan on May 14 2009 for Control Events
        string sql = "select * from sy_pg_ctr_evnt where page_id = '" + dsePageInfo.Tables["Page"].Rows[0]["af_row_id"].ToString() + "'";
        DataTable dtaPgctrevnt = HDD.GetDataTable(sql, "ControlEvents").Copy();
        dsePageInfo.Tables.Add(dtaPgctrevnt);
        //
        
        // Added by srivatsan on July 20 2009 for Getting required fields
        string sql1 = "select B.attrib_id,is_primary from sy_obj A, sy_obj_att B where A.af_row_id = B.object_id and A.object_id ='"+dsePageInfo.Tables["Page"].Rows[0]["object_id"].ToString()+"'";
        DataTable dtaObjAtt = HDD.GetDataTable(sql1,"ObjectAttributes").Copy();
        dsePageInfo.Tables.Add(dtaObjAtt);
        //

        // Added by Srivatsan on Sep 29 2009 for Notes changes to get notes data
        DataTable dtaObjNotes = CommonFunctions.GetTableFromPageInfo(strPagesetRowId, "sy_obj").Copy();
        if (dtaObj == null || dtaObj.Rows.Count == 0)
        {
            sql1 = "select * from sy_obj where object_id = '" + dsePageInfo.Tables["Page"].Rows[0]["object_id"].ToString() + "'";
            dtaObjNotes = HDD.GetDataTable(sql1, "ObjectNotes").Copy();
        }
        else
            dtaObjNotes.TableName = "ObjectNotes";
        dsePageInfo.Tables.Add(dtaObjNotes.Copy());

        if (page_id.Trim() != "")
        {
            sql1 = "select * from sy_pg_rel_info where page_id = '"+page_id+"' and active = 1 order by seq";
            DataTable dtaPageRelatedInfo = HDD.GetDataTable(sql1,"PageRelatedRecords").Copy();
            dsePageInfo.Tables.Add(dtaPageRelatedInfo);

            DataTable dtaPageRelatedQueries = GetRelatedRecordsQueries(dtaPageRelatedInfo);
            
            if (dtaPageRelatedQueries != null)
            {
                dtaPageRelatedQueries.TableName = "PageRelatedRecordQueries";
                dsePageInfo.Tables.Add(dtaPageRelatedQueries.Copy());
            }
            
        }

        if (page_id.Trim() != "")
        {
            sql1 = "select * from sy_actions where page_id = '" + page_id + "' and active = 1 order by seq";
            dsePageInfo.Tables.Add(HDD.GetDataTable(sql1, "Page Actions").Copy());
        }

        /**************Added By Dhanapal on DEC292010**********************/

        strSql = "select * from sy_pgs_mode where pageset_id = '" + strPageSetId + "'";
        dtaObj = HDD.GetDataTable(strSql, "PageModes").Copy();
        dsePageInfo.Tables.Add(dtaObj);


        strSql = "select * from sy_pgs_mode_fields where mode_id in ( select af_row_id from sy_pgs_mode where pageset_id = '" + strPageSetId + "')";
        dtaObj = HDD.GetDataTable(strSql, "PageModesFields").Copy();
        dsePageInfo.Tables.Add(dtaObj);

        /******************************************************************/

        return dsePageInfo;
    }

    //Added by srivatsan for Getting Queries from the query object
    private DataTable GetRelatedRecordsQueries(DataTable dtaPageRelatedInfo)
    {
        DataTable dtPageRelatedQueries = null;
        try
        {
            if (dtaPageRelatedInfo != null)
            {
                if (dtaPageRelatedInfo.Rows.Count > 0)
                {
                    DataRow[] drrRelatedQueries = dtaPageRelatedInfo.Select("type='query'");
                    if (drrRelatedQueries.Length > 0)
                    {
                        string query_af_row_id = "";
                        foreach (DataRow drr in drrRelatedQueries)
                        {
                            if (drr["value"].ToString().Trim() != "")
                            {
                                query_af_row_id = query_af_row_id + "'" + drr["value"].ToString() + "',";
                            }
                        }
                        if (query_af_row_id.EndsWith(","))
                        {
                            query_af_row_id = query_af_row_id.Substring(0, query_af_row_id.Length - 1);
                        }

                        if (query_af_row_id.Trim() != "")
                        {
                            string sql = "select af_row_id,report_base_sql from sy_report_sql where af_row_id in (" + query_af_row_id + ")";
                            dtPageRelatedQueries = HDD.GetDataTable(sql).Copy();

                        }

                    }
                }
            }
        }
        catch(Exception ex)
        {
            LogWriter.WriteLog("Error : GetRelatedRecordsQueries: "+ ex.Message);
        }
        finally
        {

        }
        return dtPageRelatedQueries;
    }

    private DataTable GetCtrlInfo(DataTable dtaPage, ref DataTable dtPageKey, string strPgsId)
    {
        DataTable dtaPageCtrl = new DataTable();
        string strSql = "";
        string strPageId = "";
        DataTable dtaObj = new DataTable();
        strPageId = dtaPage.Rows[0]["page_id"].ToString();
        string sAF_ROW_ID = dtaPage.Rows[0]["af_row_id"].ToString();
        //strSql = "select * from sy_pg_ctr where page_id='" + strPageId.Replace("'", "''") + "' order by control_seq";
        strSql = "select * from sy_pg_ctr where page_id='" + sAF_ROW_ID + "' order by control_seq";
        dtaPageCtrl = HDD.GetDataTable(strSql);
        getPageKeyValues(ref dtPageKey, sAF_ROW_ID, dtaPage.Rows[0]["page_table_name"].ToString(), strPgsId);
        for (int i = 1; i < dtaPage.Rows.Count; i++)
        {
            strPageId = dtaPage.Rows[i]["page_id"].ToString();
            sAF_ROW_ID = dtaPage.Rows[i]["af_row_id"].ToString();
            getPageKeyValues(ref dtPageKey, sAF_ROW_ID, dtaPage.Rows[i]["page_table_name"].ToString(), strPgsId);
            //strSql = "select * from sy_pg_ctr where page_id='" + strPageId.Replace("'", "''") + "' order by control_seq";
            strSql = "select * from sy_pg_ctr where page_id='" + sAF_ROW_ID + "' order by control_seq";
            dtaObj = HDD.GetDataTable(strSql);
            dtaPageCtrl.Merge(dtaObj);
        }
        dtaPageCtrl.TableName = "PageCtrl";
        return dtaPageCtrl.Copy();
    }

    private DataTable getPageKeyTable()
    {
        DataTable dtKeys = null;
        try
        {
            DataColumn dcPage_Id = new DataColumn("page_id");
            DataColumn dcPage_Key = new DataColumn("page_key");
            DataColumn dcPage_Key_Seq = new DataColumn("page_key_seq");
            dtKeys = new DataTable("PageKeys");
            dtKeys.Columns.Add(dcPage_Id);
            dtKeys.Columns.Add(dcPage_Key);
            dtKeys.Columns.Add(dcPage_Key_Seq);
        }

        catch (Exception ex)
        {
            dtKeys = null;
            LogWriter.WriteLog("getPageKeyTable : Error : " + ex.Message);
        }
        return dtKeys;
    }

    public DataTable getMasterDetailKey(string TableName, string strPgsId)
    {
        string sSQL = "Select af_row_id from sy_obj where object_table = '" + TableName + "'";
        HDDatabase hdd = new HDDatabase();
        string sRowId = CommonFunctions.GetColValueFromPageInfo(strPgsId, "sy_obj", "af_row_id", "object_table = '" + TableName + "'");
        if (sRowId == "") sRowId = hdd.GetColumnValue(sSQL);
        sSQL = "Select * from sy_obj_mas_detail where object_id = '" + sRowId + "'";
        return hdd.GetDataTable(sSQL).Copy();
    }

    private void updateAF_ROW_KEY(ref DataTable dtPageKey, string AF_ROW_ID)
    {
        foreach (DataRow dr in dtPageKey.Rows)
            dr["af_row_id"] = AF_ROW_ID;
    }

    private void getPageKeyValues(ref DataTable dtPageKey, string AF_ROW_ID, string Table_Name, string strPgsId)
    {
        try
        {
            //drKey["page_id"] = AF_ROW_ID;
            dtPageKey = getMasterDetailKey(Table_Name, strPgsId);
            if (dtPageKey.Rows.Count > 0)
                updateAF_ROW_KEY(ref dtPageKey, AF_ROW_ID);
            //if (sTemp == "") sTemp = "af_row_id";
            //drKey["page_key"] = sTemp;
            //dtPageKey.Rows.Add(drKey);
        }
        catch (Exception ex)
        {
            LogWriter.WriteLog("getPageKeyValues : Error : " + ex.Message);
        }
    }

    public DataSet GetPageSetPagesSrchInfo(string strPagesetRowId)
    {
        DataSet dsePageInfo = new DataSet();
        DataTable dtaObj;
        string sAF_ROW_ID= "";
        string pageid = "";
        strPagesetRowId = strPagesetRowId.Replace("'", "''");
        string strSql = "select * from sy_pgs where pageset_id='" + strPagesetRowId + "'";
        DataTable dtPageSet = CommonFunctions.GetTableFromPageInfo(strPagesetRowId, "sy_pgs").Copy();
        if (dtPageSet == null || dtPageSet.Rows.Count == 0)
            dsePageInfo.Tables.Add(HDD.GetDataTable(strSql, "PageSet").Copy());
        else
        {
            dtPageSet.TableName = "PageSet";
            dsePageInfo.Tables.Add(dtPageSet.Copy());
        }
        if (dsePageInfo.Tables.Count != 0)
        {
            if (dsePageInfo.Tables["PageSet"].Rows.Count != 0)
            {
                sAF_ROW_ID = dsePageInfo.Tables["PageSet"].Rows[0]["af_row_id"].ToString();
                string strPageSetId = sAF_ROW_ID;
                //Query needs to modify using innerjoin and needs to store with sql store
                //strSql = "select a.* from sy_pg a,sy_pgs_mem b where a.page_id=b.page_id and b.pageset_id ='" + strPagesetRowId + "' order by b.page_seq";
                dtaObj = CommonFunctions.GetTableFromPageInfo(strPagesetRowId, "sy_pg").Copy();
                if (dtaObj == null || dtaObj.Rows.Count == 0)
                {
                    strSql = "select a.* from sy_pg a,sy_pgs_mem b where a.page_id=b.page_id and b.pageset_id ='" + sAF_ROW_ID + "' order by b.page_seq";
                    dtaObj = HDD.GetDataTable(strSql, "Page").Copy();
                }
                else
                    dtaObj.TableName = "Page";
                dsePageInfo.Tables.Add(dtaObj.Copy());

                if (dtaObj != null) // Added by srivatsan for action button
                {
                    if (dtaObj.Rows.Count > 0)
                    {
                        pageid = dtaObj.Rows[0]["af_row_id"].ToString();
                    }
                }
                
                //strSql = "select a.** from sy_pg_key a,sy_pgs_mem b where a.page_id=b.page_id and b.pageset_id ='" + strPagesetRowId + "'";
                //strSql = "select a.* from sy_pg_key a,sy_pgs_mem b where a.page_id=b.page_id and b.pageset_id ='" + sAF_ROW_ID + "'";
                ////strSql = "select a.* from sy_pg_key a,sy_pgs_mem b, sy_pg c where a.page_id=c.af_row_id and c.page_id=b.page_id and b.pageset_id ='" + sAF_ROW_ID + "'";

                ////dsePageInfo.Tables.Add(HDD.GetDataTable(strSql, "PageKeys").Copy());

                DataTable dtPageKey = null;
                //dsePageInfo.Tables.Add(dtPageKey);
                //strSql = "select * from sy_pgs_srh where page_id in (select page_id from sy_pgs_mem where pageset_id='" + strPagesetRowId + "')";
                // included page control table to incldue bind control id
                //strSql = "select * from sy_pgs_srh a, sy_pg_ctr b where a.page_id=b.page_id and a.search_attrib_id= b.control_attrib_id and a.page_id in (select page_id from sy_pgs_mem where pageset_id='" + strPagesetRowId + "')";
                //strSql = "select * from sy_pgs_srh a, sy_pg_ctr b where a.page_id=b.page_id and a.search_attrib_id= b.control_attrib_id and a.page_id in (select page_id from sy_pgs_mem where af_row_id='" + sAF_ROW_ID + "')";
                
                //to be changed from here
                //strSql = "select * from sy_pgs_srh a, sy_pg_ctr b where a.page_id=b.page_id and a.search_attrib_id= b.control_attrib_id and a.page_id in (select page_id from sy_pgs_mem where pageset_id='" + sAF_ROW_ID + "') and a.pageset_id = '" + sAF_ROW_ID + "'";
                //strSql = "select * from sy_pgs_srh a, sy_pg_ctr b inner join sy_pg c on c.af_row_id = b.page_id where a.page_id=b.page_id and a.search_attrib_id= b.control_attrib_id and a.page_id in (select page_id from sy_pgs_mem where pageset_id='" + sAF_ROW_ID + "') and a.pageset_id = '" + sAF_ROW_ID + "'";
                strSql = "select * from sy_pgs_srh a, sy_pg_ctr b inner join sy_pg c on c.af_row_id = b.page_id where a.page_id=c.page_id and a.search_attrib_id= b.control_attrib_id and a.page_id in (select page_id from sy_pgs_mem where pageset_id='" + sAF_ROW_ID + "') and a.pageset_id = '" + sAF_ROW_ID + "'";
                dsePageInfo.Tables.Add(HDD.GetDataTable(strSql, "PageSrch").Copy());
                foreach (DataRow dr in dtaObj.Rows)
                {
                    sAF_ROW_ID = dr["af_row_id"].ToString();
                    strSql = "select * from sy_pg_ctr where page_id='" + sAF_ROW_ID  + "'";
                    DataTable dtTemp = null;
                    dtTemp = HDD.GetDataTable(strSql, sAF_ROW_ID);
                    dsePageInfo.Tables.Add(dtTemp.Copy());
                    dtTemp.TableName = "Page Controls";
                    dsePageInfo.Tables.Add(dtTemp.Copy());
                    getPageKeyValues(ref dtPageKey, sAF_ROW_ID, dr["page_table_name"].ToString(), strPagesetRowId);
                }
                dtPageKey.TableName = "PageKeys";
                dsePageInfo.Tables.Add(dtPageKey);

                if (pageid.Trim() != "")
                {
                    strSql = "select * from sy_actions where page_id = '" + pageid + "' and active = 1 order by seq";
                    dsePageInfo.Tables.Add(HDD.GetDataTable(strSql,"Page Actions").Copy());
                }

                /**************Added By Dhanapal on DEC292010**********************/

                strSql = "select * from sy_pgs_mode where pageset_id = '" + strPageSetId + "'";
                dtaObj = HDD.GetDataTable(strSql, "PageModes").Copy();
                dsePageInfo.Tables.Add(dtaObj);


                strSql = "select * from sy_pgs_mode_fields where mode_id in ( select af_row_id from sy_pgs_mode where pageset_id = '" + strPageSetId + "')";
                dtaObj = HDD.GetDataTable(strSql, "PageModesFields").Copy();
                dsePageInfo.Tables.Add(dtaObj);

                /******************************************************************/
            }
        }
        return dsePageInfo;
    }

    /// <summary>
    /// function to find out the pages involved in a pageset
    /// </summary>
    /// <param name="sPagesetId"></param>
    /// <returns></returns>
    public DataSet pageEventHandler(string Page_Id)
    {
        DataSet dsPageEvents = new DataSet();
        try
        {
            getEventsForPage(Page_Id, dsPageEvents);
            //string sSql = "Select * from sy_pgs_mem where pageset_id='" + sPagesetId.Replace("'", "''") + "'";
            //DataTable dtPagesForPageset = HDD.GetDataTable(sSql, "page_events").Copy();
            //foreach (DataRow dr in dtPagesForPageset.Rows)
            //    getEventsForPage(dr["page_id"].ToString().Replace("'", "''"), dsPageEvents);
        }

        catch (Exception ex)
        {
            LogWriter.WriteLog(ex.Message);
        }

        return dsPageEvents;
    }

    /// <summary>
    /// function to find out the events for a page
    /// </summary>
    /// <param name="sPageId"></param>
    /// <param name="dsPageEvents"></param>
    private void getEventsForPage(string Page_Id, DataSet dsPageEvents)
    {
        string AF_ROW_ID = CommonFunctions.GetColValueFromPageInfo((HttpContext.Current.Request.QueryString["pgs_id"] == null ? "" : HttpContext.Current.Request.QueryString["pgs_id"].ToString()), "sy_pg", "af_row_id", "page_id='" + Page_Id.Replace("'", "''") + "'");
        if (AF_ROW_ID == "") AF_ROW_ID = HDD.GetColumnValue("select af_row_id from sy_pg where page_id='" + Page_Id.Replace("'", "''") + "'");
        string sSql = "select * from sy_pg_evnt where page_id='" + AF_ROW_ID + "' and page_event <> ''";
        DataTable dtPageEvents = HDD.GetDataTable(sSql, Page_Id).Copy();
        if (dtPageEvents != null)
        {
            dsPageEvents.Tables.Add(dtPageEvents);
        }
    }

    private string getColumnValue(ref DataSet dsDataSet, string Tablename, string whereFieldname, string Value, string Fieldname)
    {
        string sValue = "";
        try
        {
            if (dsDataSet.Tables.Contains(Tablename))
            {
                DataRow[] drr = dsDataSet.Tables[Tablename].Select(whereFieldname + "='" + Value + "'");
                if (drr.Length > 0)
                    sValue = drr[0][Fieldname].ToString();
            }
        }
        catch (Exception ex)
        {
            LogWriter.WriteLog("getColumnValue : Error :" + ex.Message);
        }
        return sValue;
    }

    private bool checkForReadOnlyControl(ref DataSet PageInfo, string Control_Id)
    {
        bool bReturn = false;

        try
        {
            string sAttribute = Control_Id.Substring(Control_Id.IndexOf("].[") + 3, Control_Id.Length - (Control_Id.IndexOf(".") + 3));
            string sRead_Only = getColumnValue(ref PageInfo, "PageCtrl", "control_attrib_id", sAttribute, "read_only");
            if (sRead_Only != "")
                bReturn = bool.Parse(sRead_Only);
        }

        catch (Exception ex)
        {
            LogWriter.WriteLog(ex.Message);
        }

        return bReturn;
    }

    public bool triggerEvent(DataSet dsPageEvents, string sPageEvent, ref Hashtable hsAssignControls, ref DataSet PageInfo, HtmlForm frmObj)
    {
        bool bReturn = true;
        Master objMaster = new Master();
        try
        {
            string sFunctionName = "";
            foreach (DataTable dt in dsPageEvents.Tables)
            {
                DataRow[] drEvents = null;
                if (dt.Columns.Contains("event_sequence"))
                    drEvents = dt.Select("", "event_sequence asc");
                else
                    drEvents = dt.Select();

                foreach (DataRow dr in drEvents)
                    if (dr["page_event"].ToString().ToLower() == sPageEvent.ToLower())
                    {
                        if (sPageEvent.ToLower() == "pl")
                            if (frmObj != null)

                                // Added by Srivatsan for testing
                                if (HttpContext.Current.Request.Url.AbsoluteUri.ToLower().Contains("transdoc.aspx"))
                                {

                                }
                                else
                                {
                                    if (frmObj.Page.IsPostBack && !checkForReadOnlyControl(ref PageInfo, dr["event_ass_cont"].ToString())) continue;
                                }

                        string sPageId = dt.TableName;//HDD.GetColumnValue("select page_id from sy_pg where af_row_id='" + dt.Rows[0]["af_row_id"].ToString() + "'");
                        object oReturn = null;
                        string sParam = "";
                        if (dr["event_type"].ToString() == "av")
                        {
                            sParam = dr["event_function"].ToString();
                            string tempsParam = dr["event_function"].ToString();
                            if (sParam.StartsWith("{!") && sParam.EndsWith("}"))
                            {
                                tempsParam = tempsParam.Remove(tempsParam.Length - 1, 1);
                                tempsParam = tempsParam.Remove(0, 2);
                                string sSysValue = HDD.GetColumnValue("Select sy_var_value from sy_var where sy_var_name = '" + tempsParam + "'");
                                
                                if (sSysValue.Contains("["))
                                {
                                    string sObject_id = sSysValue.Substring(sSysValue.IndexOf("[") + 1, sSysValue.IndexOf(".", sSysValue.IndexOf("[") + 1) - (sSysValue.IndexOf("[") + 1));
                                    string sSql = "Select object_table from sy_obj where object_id='" + sObject_id + "'";
                                    string sObject_table = HDD.GetColumnValue(sSql);
                                    sSql = "select page_id from sy_pg where object_id='" + sObject_id + "'";
                                    string sPage_id = HDD.GetColumnValue(sSql);
                                    sSql = "Select multiple_recs_page from sy_pg where page_id = '" + sPage_id + "'";
                                    string stemp = HDD.GetColumnValue(sSql);
                                    bool is_multi_rec_page = Convert.ToBoolean(stemp == "" ? "false" : stemp);
                                    int iStartPos = sSysValue.IndexOf("[") + 1;
                                    iStartPos = sSysValue.IndexOf(".", iStartPos) + 1;
                                    int iEndPos = sSysValue.Length - 1;
                                    iEndPos = sSysValue.IndexOf("]", iStartPos);
                                    //string sField = sSysValue.Substring(sSysValue.IndexOf(".") + 1, sSysValue.Length - 1 - (sSysValue.IndexOf(".") + 1));
                                    string sField = sSysValue.Substring(iStartPos, iEndPos - iStartPos);
                                    tempsParam = sObject_table + "." + sField;

                                    if (is_multi_rec_page)
                                    {
                                        string sCtrlId = dr["event_ass_cont"].ToString();
                                        for (int i = 0; i < PageInfo.Tables[sPage_id].Rows.Count; i++)
                                        {
                                            string svalue = HDD.GetColumnValue(sSysValue.Replace("[" + sObject_id + "." + sField + "]", PageInfo.Tables[sPage_id].Rows[i][sField].ToString()));
                                            hsAssignControls.Add(sCtrlId + "_" + i.ToString(), svalue);
                                        }
                                    }
                                    else
                                    {
                                        string sCtrlId = dr["event_ass_cont"].ToString();
                                        string svalue = HDD.GetColumnValue(sSysValue.Replace("[" + sObject_id + "." + sField + "]", PageInfo.Tables[sPage_id].Rows[0][sField].ToString()));
                                        hsAssignControls.Add(sCtrlId, svalue);
                                        oReturn = svalue;
                                        //if (GlobalValues.hsSysVar.Contains(sParam))
                                        //{
                                        //    oReturn = GlobalValues.hsSysVar[sParam];
                                        //}
                                    }
                                }
                                else
                                {
                                    Hashtable hsSysVar = (Hashtable)HttpContext.Current.Session["SysVar"];
                                    if (hsSysVar.Contains(sParam))
                                    {
                                        oReturn = hsSysVar[sParam];
                                    }
                                    /*if (GlobalValues.hsSysVar.Contains(sParam))
                                    {
                                        oReturn = GlobalValues.hsSysVar[sParam];
                                    }*/
                                }
                            }
                            else if (sParam.StartsWith("{@") && sParam.EndsWith("}"))
                            {
                                //sParam = sParam.Remove(sParam.Length - 1, 1);
                                //sParam = sParam.Remove(0, 2);
                                security hds = new security();
                                Hashtable hsAppVar = new Hashtable();
                                hds.loadAppVariables(ref hsAppVar);
                                hds = null;
                                if (hsAppVar.Contains(sParam))
                                {
                                    oReturn = hsAppVar[sParam];
                                }
                                //if (GlobalValues.hsAppVar.Contains(sParam))
                                //{
                                //    oReturn = GlobalValues.hsAppVar[sParam];
                                //}
                            }
                            else if (sParam.StartsWith("[") && sParam.EndsWith("]"))
                            {
                                string sObject_id = sParam.Substring(sParam.IndexOf("[") + 1, sParam.IndexOf(".") - (sParam.IndexOf("[") + 1));
                                string sSql = "Select object_table from sy_obj where object_id='" + sObject_id + "'";
                                string sObject_table = HDD.GetColumnValue(sSql);
                                sSql = "Select multiple_recs_page from sy_pg where page_id = 'p_" + sObject_id + "'";
                                string stemp = HDD.GetColumnValue(sSql);
                                bool is_multi_rec_page = Convert.ToBoolean(stemp == "" ? "false" : stemp);
                                string sField = sParam.Substring(sParam.IndexOf(".") + 1, sParam.Length - 1 - (sParam.IndexOf(".") + 1));
                                sParam = sObject_table + "." + sField;

                                if (is_multi_rec_page)
                                {
                                    string sCtrlId = dr["event_ass_cont"].ToString();
                                    for(int i=0;i<PageInfo.Tables["p_" + sObject_id].Rows.Count ;i++)
                                    {
                                        //GlobalValues.hsSysVar.Add(sParam + "_" + i.ToString(), PageInfo.Tables["p_" + sObject_id].Rows[i][sField].ToString());
                                        hsAssignControls.Add(sCtrlId + "_" + i.ToString(), PageInfo.Tables["p_" + sObject_id].Rows[i][sField].ToString()); 
                                    }
                                }
                                else
                                {
                                    Hashtable hsSysVar = (Hashtable)HttpContext.Current.Session["SysVar"];
                                    if (hsSysVar.Contains(sParam))
                                        oReturn = hsSysVar[sParam].ToString();
                                    else
                                        oReturn = "";
                                    /*if (GlobalValues.hsSysVar.Contains(sParam))
                                        oReturn = GlobalValues.hsSysVar[sParam].ToString();
                                    else
                                        oReturn = "";*/
                                }
                            }
                        }
                        else
                        {
                            oReturn = invokeEvent(dr["event_function"].ToString(), PageInfo.Tables[sPageId], ref sFunctionName, dr["event_type"].ToString(), ref hsAssignControls, dr);
                        }
                        if (dr["event_type"].ToString() == "a")
                        {
                            string sCtrlId = dr["event_ass_cont"].ToString();
                            if (!hsAssignControls.Contains(sCtrlId))
                                hsAssignControls.Add(sCtrlId, oReturn);
                            else
                                hsAssignControls[sCtrlId] =  oReturn;
                        }
                        else if (dr["event_type"].ToString() == "av")
                        {
                            string sCtrlId = dr["event_ass_cont"].ToString();
                            if (!hsAssignControls.Contains(sCtrlId))
                                hsAssignControls.Add(sCtrlId, oReturn);
                            else
                                hsAssignControls[sCtrlId] = oReturn;
                        }
                        else if (dr["event_type"].ToString() == "v")
                        {
                            if (oReturn != null)
                            {
                                bReturn = Convert.ToBoolean(oReturn);
                                if (bReturn == false)
                                {
                                    if (oReturn.ToString() != "")
                                    {
                                        sFunctionName = sFunctionName + " returned " + oReturn.ToString();
                                        if (objMaster.DeleteMessage == "")
                                            objMaster.DeleteMessage = sFunctionName;
                                    }
                                    return bReturn;
                                }
                            }
                        }
                    }
            }
        }

        catch (Exception ex)
        {
            LogWriter.WriteLog("triggerEvent : Error : " + ex.Message);
            objMaster.DeleteMessage = ex.Message;
            bReturn = false;
        }
        return bReturn;
    }

    public object invokeEvent(string EventFunction, DataTable PageInfo, ref string FunctionName, string Event_Type, ref Hashtable hsAssignControls, DataRow drEventInfo)
    {
        DataRow[] drParams = null;
        DataTable dtParams = null;

        object oRet = null;
        if (Event_Type == "ad")
        {
            string sEventCode = "";
            switch (drEventInfo["page_event"].ToString().ToUpper())
            {
                case "PL":
                    sEventCode = "LOAD";
                    break;
                case "PS":
                    sEventCode = "INSERT";
                    break;
                case "PU":
                    sEventCode = "UPDATE";
                    break;
                case "PAS":
                    sEventCode = "AFTER_INSERT";
                    break;
                case "PAU":
                    sEventCode = "AFTER_UPDATE";
                    break;
            }

            DataRow drFunction = HDD.GetTableRow("select * from sy_page_events_advanced where af_row_id = '" + drEventInfo["event_function"].ToString() + "'");
            oRet = runMethod(HttpContext.Current.Server.MapPath(".") + "\\CustomFunctions\\" + drFunction["dll_name"].ToString().Trim() + ".dll", drFunction["namespace"].ToString().Trim() + "." + drFunction["class"].ToString().Trim(), drFunction["function_name"].ToString().Trim(), sEventCode, PageInfo);
            drFunction = null;
        }
        else
        {
            oRet = executeEvent(getEvent(EventFunction, ref FunctionName, ref drParams, ref dtParams), ref drParams, ref dtParams, PageInfo, ref FunctionName, Event_Type, ref hsAssignControls, drEventInfo);
        }

        return oRet;
    }

    private DataRow getEvent(string EventFunction, ref string sFunctionName, ref DataRow[] Parameters, ref DataTable Params)
    {
        string sSql = "select fr_name from my_function_register where id='" + EventFunction + "'";
        sFunctionName = HDD.GetColumnValue(sSql);

        sSql = "select fr_selected_fnExp from my_function_register where id='" + EventFunction + "'";
        string sTemp = HDD.GetColumnValue(sSql);
        sFunctionName = "'" + sFunctionName + "' (" + sTemp + ")";

        string sFunction_Id = HDD.GetColumnValue("select af_row_id from my_function_register where id='" + EventFunction + "'");

        sSql = "select * from my_fn_register_child where id='" + sFunction_Id + "'";
        DataRow[] drParams = HDD.GetDataTable(sSql).Select();

        sSql = "select * from my_fn_register_child where id ='" + sFunction_Id + "'";
        Params = HDD.GetDataTable(sSql);

        sSql = "select * from my_function where fn_name='" + sTemp + "'";
        return HDD.GetTableRow(sSql);
    }

    private string getExpression(ref DataRow dr, DataRow drEventInfo, ref Hashtable hsAssignControl, ref DataTable PageInfo)
    {
        string sFunction = "";

        try
        {
            //sFunction = dr["fn_code"].ToString();
            sFunction = dr["fn_code"].ToString();

            string sSql = "Select * from sy_pg where page_id='" + PageInfo.TableName  + "'";
            DataRow drPageObject = HDD.GetTableRow(sSql);
            string stemp = drPageObject["multiple_recs_page"].ToString();
            bool is_multi_rec_page = Convert.ToBoolean(stemp == "" ? "false" : stemp);
            string sObjectTable = drPageObject["page_table_name"].ToString();
            string sObjectId = drPageObject["object_id"].ToString();

            if (is_multi_rec_page)
            {
                MSScriptControl.ScriptControl objScriptCtrl = new MSScriptControl.ScriptControl();

                objScriptCtrl.Language = "vbscript";

                string sCtrlId = drEventInfo["event_ass_cont"].ToString();

                for (int iRowCount = 0; iRowCount < PageInfo.Rows.Count; iRowCount++)
                {
                    string sTempExpression = sFunction;
                    DataRow drInfo = PageInfo.Rows[iRowCount];

                    foreach (DataColumn dc in PageInfo.Columns)
                    {
                        string sTempKey = "[" + sObjectId + "." + dc.ColumnName + "]";
                        if (sTempExpression.IndexOf(sTempKey) != -1)
                        {
                            sTempExpression = sTempExpression.Replace(sTempKey, drInfo[dc.ColumnName].ToString());
                        }
                    }

                    object objRet = objScriptCtrl.Eval(sTempExpression);
                    if (!hsAssignControl.Contains(sCtrlId + iRowCount.ToString()))
                        hsAssignControl.Add(sCtrlId + "_" + iRowCount.ToString(), objRet);
                    else
                        hsAssignControl[sCtrlId + "_" + iRowCount.ToString()] = objRet;
                }
            }
            else
            {
                Hashtable hsSysVar = (Hashtable)HttpContext.Current.Session["SysVar"];
                if (hsSysVar != null)//if (GlobalValues.hsSysVar != null)
                {
                    foreach (string sKey in hsSysVar.Keys)//foreach (string sKey in GlobalValues.hsSysVar.Keys)
                    {
                        if (sKey.Contains("."))
                        {
                            /*string sObjectTable = sKey.Substring(0, sKey.IndexOf("."));
                            sSql = "Select object_id from sy_obj where object_table='" + sObjectTable.Replace("'", "''") + "'";
                            string sObjectId = HDD.GetColumnValue(sSql);*/
                            /*if (is_multi_rec_page)
                            {
                                MSScriptControl.ScriptControl objScriptCtrl = new MSScriptControl.ScriptControl();

                                objScriptCtrl.Language = "vbscript";

                                string sCtrlId = drEventInfo["event_ass_cont"].ToString();
                                for (int iRowCount = 0; iRowCount < PageInfo.Rows.Count; iRowCount++)
                                {
                                    DataRow drInfo = PageInfo.Rows[iRowCount];

                                    string sTempKey = "[" + sKey.Replace(sObjectTable, sObjectId) + "]";
                                    if (sFunction.IndexOf(sTempKey) != -1)
                                    {
                                        sFunction = sFunction.Replace(sTempKey, GlobalValues.hsSysVar[sKey].ToString());
                                    }

                                    object objRet = objScriptCtrl.Eval(sFunction);
                                    if (!hsAssignControl.Contains(sCtrlId + iRowCount.ToString()))
                                        hsAssignControl.Add(sCtrlId + "_" + iRowCount.ToString(), objRet);
                                    else
                                        hsAssignControl[sCtrlId + "_" + iRowCount.ToString()] = objRet;
                                }
                            }
                            else
                            {*/
                            string sTempKey = "[" + sKey.Replace(sObjectTable, sObjectId) + "]";
                            if (sFunction.IndexOf(sTempKey) != -1)
                            {
                                sFunction = sFunction.Replace(sTempKey, hsSysVar[sKey].ToString());//sFunction = sFunction.Replace(sTempKey, GlobalValues.hsSysVar[sKey].ToString());
                            }
                            //}
                        }
                    }
                }
            }
        }

        catch (Exception ex)
        {
            LogWriter.WriteLog("getExpression : error : " + ex.Message);
        }
        return sFunction;
    }

    private object executeEvent(DataRow dr, ref DataRow[] Parameters, ref DataTable dtParams, DataTable PageInfo, ref string FunctionName, string Event_Type, ref Hashtable hsAssignControls, DataRow drEventInfo)
    {
        object objRet = null;
        try
        {
            FunctionName = "The Event " + FunctionName;
            if (dr != null)
            {
                MSScriptControl.ScriptControl objScriptCtrl = new MSScriptControl.ScriptControl();

                objScriptCtrl.Language = "vbscript";

                if (dr["fn_type"].ToString().ToLower() == "expression")
                {
                    string sExpression = getExpression(ref dr, drEventInfo, ref hsAssignControls, ref PageInfo);
                    
                    objRet = objScriptCtrl.Eval(sExpression);
                }
                else
                {
                    string sFunctionCode = dr["fn_code"].ToString();
                    objScriptCtrl.AddCode(sFunctionCode);
                    foreach (MSScriptControl.Procedure Proc in objScriptCtrl.Procedures)
                    {
                        string sFuncName = Proc.Name;

                        object[] oParams = new object[Proc.NumArgs];
                        if (Proc.NumArgs > 0)
                        {
                            FunctionName = FunctionName + " with parameter(s) :";
                            int iStartPos = sFunctionCode.IndexOf("(", sFunctionCode.ToLower().IndexOf("function " + sFuncName.ToLower())) + 1;
                            int iEndPos = sFunctionCode.IndexOf(")", iStartPos);
                            string[] sTemp = sFunctionCode.Substring(iStartPos, iEndPos - iStartPos).Split(',');
                            for (int iArg = 0; iArg < sTemp.Length; iArg++)
                            {
                                DataRow[] dtTemp = dtParams.Select("param_name='" + sTemp[iArg].Trim() + "'");
                                if (dtTemp.Length > 0)
                                {
                                    string sParam = dtTemp[0]["param_value"].ToString();
                                    FunctionName = FunctionName + " " + sParam + ",";
                                    if (sParam.StartsWith("{!") && sParam.EndsWith("}"))
                                    {
                                        Hashtable hsSysVar = (Hashtable)HttpContext.Current.Session["SysVar"];
                                        if (hsSysVar != null)//if (GlobalValues.hsSysVar != null)
                                        {
                                            if (hsSysVar.Contains(sParam))
                                                oParams[iArg] = hsSysVar[sParam].ToString();
                                            else
                                                oParams[iArg] = "";
                                            //sParam = sParam.Remove(sParam.Length - 1, 1);
                                            //sParam = sParam.Remove(0, 2);
                                            /*if (GlobalValues.hsSysVar.Contains(sParam))
                                                oParams[iArg] = GlobalValues.hsSysVar[sParam].ToString();
                                            else
                                                oParams[iArg] = "";*/
                                        }
                                    }
                                    else if (sParam.StartsWith("{@") && sParam.EndsWith("}"))
                                    {
                                        security hds = new security();
                                        Hashtable hsAppVar = new Hashtable();
                                        hds.loadAppVariables(ref hsAppVar);
                                        hds = null;

                                        if (hsAppVar != null)
                                        {
                                            if (hsAppVar.Contains(sParam))
                                                oParams[iArg] = hsAppVar[sParam].ToString();
                                            else
                                                oParams[iArg] = "";
                                        }

                                        //if (GlobalValues.hsAppVar != null)
                                        //{
                                        //    //sParam = sParam.Remove(sParam.Length - 1, 1);
                                        //    //sParam = sParam.Remove(0, 2);
                                        //    if (GlobalValues.hsAppVar.Contains(sParam))
                                        //        oParams[iArg] = GlobalValues.hsAppVar[sParam].ToString();
                                        //    else
                                        //        oParams[iArg] = "";
                                        //}
                                    }

                                    else if (sParam.StartsWith("[") && sParam.EndsWith("]") && sParam.Contains("."))//("].[")
                                    {
                                        string sObject_id = sParam.Substring(sParam.IndexOf("[") + 1, sParam.IndexOf(".") - (sParam.IndexOf("[") + 1));

                                        string sSql = "Select object_table from sy_obj where object_id='" + sObject_id + "'";
                                        string sObject_table = HDD.GetColumnValue(sSql);
                                        sSql = "Select multiple_recs_page from sy_pg where page_id = 'p_" + sObject_id + "'";
                                        string stemp = HDD.GetColumnValue(sSql);
                                        bool is_multi_rec_page = Convert.ToBoolean(stemp == "" ? "false" : stemp);
                                        sParam = sObject_table + "." + sParam.Substring(sParam.IndexOf(".") + 1, sParam.Length - 1 - (sParam.IndexOf(".") + 1));
                                        //sParam = sParam.Replace("].[", ".");
                                        if (is_multi_rec_page)
                                        {
                                            string sCtrlId = drEventInfo["event_ass_cont"].ToString();
                                            for (int iRowCount = 0; iRowCount < PageInfo.Rows.Count; iRowCount++)
                                            {
                                                DataRow drInfo = PageInfo.Rows[iRowCount];
                                                oParams[iArg] = drInfo[sParam.Substring(sParam.IndexOf(".") + 1, sParam.Length - (sParam.IndexOf(".") + 1))].ToString();
                                                objRet = objScriptCtrl.Run(sFuncName, ref oParams);
                                                //FunctionName = FunctionName + " returned " + objRet.ToString();
                                                if (Event_Type == "v")
                                                {
                                                    if (Convert.ToBoolean(objRet.ToString()) == false)
                                                    {
                                                        FunctionName = FunctionName.Remove(FunctionName.Length - 1);
                                                        return objRet;
                                                    }
                                                }
                                                else if (Event_Type == "a")
                                                {
                                                    if (!hsAssignControls.Contains(sCtrlId + "_" + iRowCount.ToString()))
                                                        hsAssignControls.Add(sCtrlId + "_" + iRowCount.ToString(), objRet);
                                                    else
                                                        hsAssignControls[sCtrlId + "_" + iRowCount.ToString()] = objRet;
                                                }
                                            }
                                        }
                                        else
                                        {
                                            Hashtable hsSysVar = (Hashtable)HttpContext.Current.Session["SysVar"];
                                            if (hsSysVar.Contains(sParam))
                                                oParams[iArg] = hsSysVar[sParam].ToString();
                                            else
                                                oParams[iArg] = "";
                                            /*if (GlobalValues.hsSysVar.Contains(sParam))
                                                oParams[iArg] = GlobalValues.hsSysVar[sParam].ToString();
                                            else
                                                oParams[iArg] = "";*/
                                        }
                                    }
                                    else
                                        oParams[iArg] = sParam;
                                }
                                else
                                    oParams[iArg] = "";
                            }
                            FunctionName = FunctionName.Remove(FunctionName.Length - 1);
                        }

                        objRet = objScriptCtrl.Run(sFuncName, ref oParams);
                        objScriptCtrl = null;
                    }
                }
            }
        }

        catch (Exception ex)
        {
            Master objMaster = new Master();
            LogWriter.WriteLog("executeEvent : Error : " + ex.Message);
            objMaster.DeleteMessage = FunctionName + " terminated execution with the error : " + ex.Message;
            objRet = false;
        }
        finally
        {
        }
        return objRet;
    }

    /*
    private object runMethod(string assemblyFileName, string typeName, string methodName, string Event, DataTable InputInfo)
    {
        Object obj;
        object objret=null;
        try
        {
            obj = LoadDLL(assemblyFileName, typeName, null);
            Type myTypeObj = obj.GetType();
            //MethodInfo myMethodInfo = myTypeObj.GetMethod("returnStr");
            MethodInfo myMethodInfo = myTypeObj.GetMethod(methodName);
            if (InputInfo != null)
            {
                foreach (DataRow dr in InputInfo.Rows)
                {
                    object[] mParam = null;
                    if (HttpContext.Current.Request["MK"] == null)
                    {
                        mParam = new object[] { HttpContext.Current.Session["dbcon"], HttpContext.Current.Session["company_id"], HttpContext.Current.Session["user_afrowid"], dr["af_row_id"].ToString(), HttpContext.Current.Request["mn"], Event };
                    }
                    else
                    {
                        mParam = new object[] { HttpContext.Current.Session["dbcon"], HttpContext.Current.Session["company_id"], HttpContext.Current.Session["user_afrowid"], HttpContext.Current.Request["MK"], HttpContext.Current.Request["mn"], Event };
                    }
                    object ob = myMethodInfo.Invoke(obj, mParam);
                    objret = ob;
                }
            }
            else
            {
                object[] mParam1 = new object[] { HttpContext.Current.Session["dbcon"], HttpContext.Current.Session["company_id"], HttpContext.Current.Session["user_afrowid"], "", HttpContext.Current.Request["mn"], Event };
                object ob1 = myMethodInfo.Invoke(obj, mParam1);
                objret = ob1;
            }
            myMethodInfo = null;
            myTypeObj = null;
        }
        catch (Exception e)
        {
            objret = null;
            LogWriter.WriteLog("Error in invoking method: " + methodName + " in assembly " + assemblyFileName + ". \r\nError:\r\n" + e.Message);
        }
        finally
        {
            obj = null;
        }
        return objret;
    }
*/

    private object runMethod(string assemblyFileName, string typeName, string methodName, string Event, DataTable InputInfo)
    {
        Object obj;
        object objret = null;
        try
        {
            obj = LoadDLL(assemblyFileName, typeName, null);
            Type myTypeObj = obj.GetType();
            //MethodInfo myMethodInfo = myTypeObj.GetMethod("returnStr");
            MethodInfo myMethodInfo = myTypeObj.GetMethod(methodName);
            if (InputInfo != null)
            {
                foreach (DataRow dr in InputInfo.Rows)
                {
                    object[] mParam = null;
                    if (HttpContext.Current.Request["MK"] == null)
                    {
                        mParam = new object[] { HttpContext.Current.Session["dbcon"], HttpContext.Current.Session["company_id"], HttpContext.Current.Session["user_afrowid"], dr["af_row_id"].ToString(), HttpContext.Current.Request["mn"], Event };
                    }
                    else
                    {
                        mParam = new object[] { HttpContext.Current.Session["dbcon"], HttpContext.Current.Session["company_id"], HttpContext.Current.Session["user_afrowid"], HttpContext.Current.Request["MK"], HttpContext.Current.Request["mn"], Event };
                    }

                    if (methodName.ToLower().EndsWith("_ver1"))
                    {
                        Array.Resize(ref mParam, mParam.Length + 1);
                        mParam.SetValue(addAdditionalParam(dr), mParam.Length - 1);
                    }

                    object ob = myMethodInfo.Invoke(obj, mParam);
                    objret = ob;
                }
            }
            else
            {
                object[] mParam1 = new object[] { HttpContext.Current.Session["dbcon"], HttpContext.Current.Session["company_id"], HttpContext.Current.Session["user_afrowid"], "", HttpContext.Current.Request["mn"], Event };

                if (methodName.ToLower().EndsWith("_ver1"))
                {
                    DataRow tempDR = null;
                    Array.Resize(ref mParam1, mParam1.Length + 1);
                    mParam1.SetValue(addAdditionalParam(tempDR), mParam1.Length - 1);
                }
                object ob1 = myMethodInfo.Invoke(obj, mParam1);
                objret = ob1;
            }
            myMethodInfo = null;
            myTypeObj = null;
        }
        catch (Exception e)
        {
            objret = null;
            LogWriter.WriteLog("Error in invoking method: " + methodName + " in assembly " + assemblyFileName + ". \r\nError:\r\n" + e.Message);
        }
        finally
        {
            obj = null;
        }
        return objret;
    }
    private object addAdditionalParam(DataRow DR)
    {
        //Function Description
        #region
        /* This function will get the below things and return as Array.
* 1. DataRow
* 2. URL Referrer
* 3. Request URL
*/
        #endregion
        //Declaration Phase
        Hashtable HParam = new Hashtable();
        string RequestURL = "";
        string URLReferer = "";

        try
        {
            if (HttpContext.Current.Request != null)
            {
                if (HttpContext.Current.Request.Url != null)
                {
                    RequestURL = HttpContext.Current.Request.Url.ToString();
                }
                if (HttpContext.Current.Request.UrlReferrer != null)
                {
                    URLReferer = HttpContext.Current.Request.UrlReferrer.ToString();
                }
            }
            HParam.Add("DataRow", DR);
            HParam.Add("RequestURL", RequestURL);
            HParam.Add("URLReferer", URLReferer);
        }
        catch (Exception ex)
        {
            LogWriter.WriteLog("Error while getting object:" + ex.Message);
        }
        return (object)HParam;
    }


    private Object getAdvancedParameters(DataRow dr)
    {
        Hashtable ht = new Hashtable();
        try
        {
            string curUrl = "";
            if (HttpContext.Current.Request != null) curUrl = HttpContext.Current.Request.Url.ToString();
            string refUrl = "";
            if (HttpContext.Current.Request != null)
            {
                refUrl = HttpContext.Current.Request.UrlReferrer.ToString();
            }
            //dr, curUrl, refUrl
            ht.Add("datarow", dr);
            ht.Add("cururl", curUrl);
            ht.Add("refurl", refUrl);            
        }
        catch (Exception e)
        {
            LogWriter.WriteLog("Error in getAdvancedParameters: " + e.Message);
        }
        return (Object)ht;
    }

    public Object LoadDLL(string DLLName, string ClassName, object[] args)
    {
        Object objTmp = null;
        try
        {
            Assembly objAssembly = Assembly.LoadFrom(DLLName);
            objTmp = objAssembly.CreateInstance(ClassName);
        }
        catch (Exception ex)
        {
            return null;
        }
        return objTmp;
    }
}
